home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 1249 / euclid.t < prev    next >
Text File  |  1997-04-18  |  871b  |  56 lines

  1. %
  2. % "euclid.t" finds the greatest common denominator 
  3. % of two integers using Euclid's algorithm
  4. %
  5. %   Sample program for the T Interpreter by:
  6. %
  7. %   Stephen R. Schmitt
  8. %   962 Depot Road
  9. %   Boxborough, MA 01719
  10. %
  11.  
  12. var x, y, z : int
  13.  
  14. program
  15.  
  16.     put "Numbers must be positive integers!"
  17.     prompt "enter numerator:"
  18.     get x
  19.     assert x > 0
  20.  
  21.     prompt "enter denominator:"
  22.     get y
  23.     assert y > 0
  24.  
  25.     z := gcd( x, y )
  26.     put "greatest common denominator = ", z
  27.  
  28.     put "smallest numerator   = ", x/z
  29.     put "smallest denominator = ", y/z
  30.  
  31. end program
  32.  
  33.  
  34. function gcd( u : int, v : int ) : int
  35.  
  36.     var t : int
  37.  
  38.     loop
  39.  
  40.         exit when u <= 0
  41.  
  42.         if u < v then
  43.  
  44.             t := u
  45.             u := v
  46.             v := t
  47.  
  48.         end if
  49.  
  50.         u := u - v
  51.  
  52.     end loop
  53.  
  54.     return v
  55.  
  56. end function